package com.artisan.admin.controllers;

import com.artisan.admin.model.certificateTypeViewModels.CertificateTypeViewModel;
import com.artisan.admin.requestModels.accountListRequestModels.AccountListRequestModel;
import com.artisan.admin.resultModels.MessageBase;
import com.artisan.admin.resultModels.accountResultModels.AccountResultModel;
import com.artisan.admin.resultModels.certificateTypeResultModels.CertificateTypeResultModel;
import com.artisan.domain.dto.AccountDto;
import com.artisan.domain.dto.CertificateTypeDto;
import com.artisan.domain.serviceInterface.ICertificateTypeService;
import com.artisan.util.DateUtil;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;

/**
 * 证书类型控制器
 */
@RequestMapping("/api/certificateType")
@RestController
@Api("证书类型模块")
@ApiOperation(value = "证书类型模块")
public class CertificateTypeController {
    @Autowired
    private ICertificateTypeService certificateTypeService;

    /**
     * 获得列表
     * @param name 名称
     * @param pageIndex 当前页
     * @param pageSize 每页数
     */
    // GET /list
    @ApiOperation(value = "获得列表")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",
                    name = "name",
                    dataType = "String",
                    value = "证书类型名称",
                    defaultValue = "浙江省"),
            @ApiImplicitParam(
                    paramType = "query",
                    name = "pageIndex",
                    dataType = "Int",
                    required = true,
                    value = "当前页",
                    defaultValue = "0"),
            @ApiImplicitParam(
                    paramType = "query",
                    name = "pageSize",
                    dataType = "Int",
                    required = true,
                    value = "每页数量",
                    defaultValue = "10")
    })
    @ApiResponses(@ApiResponse(code = 200, response = CertificateTypeResultModel.class, message = "成功"))
    @RequestMapping(value = {"/list"}, method = {RequestMethod.GET})
    public MessageBase getList(@RequestParam String name, @RequestParam int pageIndex, @RequestParam int pageSize) {
        MessageBase result = new MessageBase();
        Page<CertificateTypeDto> page = this.certificateTypeService.findList(name, pageIndex, pageSize);
        result.setPageIndex(page.getNumber());
        result.setPageSize(page.getSize());
        result.setTotalRecord(Math.toIntExact(page.getTotalElements()));

        ArrayList<CertificateTypeResultModel> models = new ArrayList<>();

        page.getContent()
                .forEach(
                        dto -> {
                            CertificateTypeResultModel model = new CertificateTypeResultModel();
                            BeanUtils.copyProperties(dto, model);
                            models.add(model);
                        });

        result.setData(models);
        return result;
    }

    /**
     * 添加
     * @param model 模型
     */
    // POST /
    @ApiOperation(value = "添加")
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(value = {"/"}, method = {RequestMethod.GET})
    public MessageBase post(@RequestBody CertificateTypeViewModel model){
        MessageBase result = new MessageBase();
        CertificateTypeDto dto = new CertificateTypeDto();
        BeanUtils.copyProperties(model, dto);
        int id = this.certificateTypeService.add(dto);
        return result;
    }

    /**
     * 获得详情
     * @param id id
     */
    // GET /{1}
    @ApiOperation(value = "获得详情")
    @ApiResponses(@ApiResponse(code = 200, response = CertificateTypeResultModel.class, message = "成功"))
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.GET})
    public MessageBase get(@PathVariable int id){
        MessageBase result = new MessageBase();
        CertificateTypeDto dto = this.certificateTypeService.find(id);
        CertificateTypeResultModel model = new CertificateTypeResultModel();
        BeanUtils.copyProperties(dto, model);
        result.setData(model);
        return result;

    }

    /**
     * 更新数据
     * @param id id
     * @param model 模型
     */
    // PUT /{1}
    @ApiOperation(value = "获得详情")
    @ApiResponses(@ApiResponse(code = 200, response = CertificateTypeResultModel.class, message = "成功"))
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.PUT})
    public MessageBase put(@PathVariable int id, @RequestBody CertificateTypeViewModel model){
        MessageBase result= new MessageBase();
        CertificateTypeDto dto = new CertificateTypeDto();
        BeanUtils.copyProperties(model, dto);
        dto.setId(id);
        boolean b = this.certificateTypeService.update(dto);
        return result;
    }

    /**
     * 删除数据
     * @param id id
     */
    // DELETE /{1}
    @ApiOperation(value = "获得详情")
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.DELETE})
    public MessageBase delete(@PathVariable int id){
        MessageBase result = new MessageBase();
        boolean b = this.certificateTypeService.delete(id);
        return result;
    }
}
